#include "stdafx.h"

#define VERSION_STRING          _T("1.0.0.1")


/* Print the list of subfolders on standard output. */
static void PrintFolders ( TCHAR *lpszFilter );
/* Print help information. */
static void PrintHelp ();


int _tmain ( int argc, _TCHAR* argv[] )
{
    // check if version display is required:
    if ( argc == 2
            && ( _tcscmp ( argv[1], _T("-v") ) == 0 || _tcscmp ( argv[1], _T("--version") ) == 0 ) )
    {
        _tprintf ( VERSION_STRING _T("\r\n") );
        return 0;
    }

    // check if explanation is required:
    if ( argc == 2
            && ( _tcscmp ( argv[1], _T("-help") ) == 0 || _tcscmp ( argv[1], _T("--help") ) == 0 ) )
    {
        PrintHelp ();
        return 0;
    }

    if ( argc == 2 )
        PrintFolders ( argv[1] );
    else
        PrintFolders ( NULL );

    return 0;
}


static void PrintFolders ( TCHAR *lpszFilter )
{
    _tfinddata_t    hFile;
    intptr_t        pFile;
    int             nResult;

    if ( lpszFilter == NULL || lpszFilter[0] == _T('\0') )
        lpszFilter = _T("*");

    // get first file from the folder:
    pFile = _tfindfirst ( lpszFilter, &hFile );

    if ( pFile < 0 )
        return;

    do
    {
        _tprintf ( _T("%s\r\n"), hFile.name );
        nResult = _tfindnext ( pFile, &hFile );
    }
    while ( nResult == 0 );
}

static void PrintHelp ()
{
    _tprintf ( _T("Pretorianie 2008 (C)\r\n") );
    _tprintf ( _T("Directory List v%s\r\n\r\n"), VERSION_STRING );
    _tprintf ( _T(" This utility prints the list of all files and folders that belong to current directory. It can also apply a speicifed filter.\r\n\r\n") );
    _tprintf ( _T("Usage:\r\n") );
    _tprintf ( _T("\tdirlist.exe [-v] [-help] {filter}\r\n\r\n") );
    _tprintf ( _T("Sample:\r\n") );
    _tprintf ( _T("\tdirlist.exe -v    - prints the current version of the program\r\n") );
    _tprintf ( _T("\tdirlist.exe -help - prints that info\r\n") );
    _tprintf ( _T("\tdirlist.exe *.txt - prints the list of text files\r\n") );
}
